<?php

namespace App\Http\Controllers\Admin;

use App\Model\Lesson;
use App\Model\LessonTag;
use App\Model\Tag;
use App\Model\Video;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class LessonController extends CommonController
{
    /**
     * Display a listing of the resource.
     * 课程列表识图
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
       $data=Lesson::OrderBy('id','DESC')->paginate(5);
       return view('admin.lesson.index',compact('data'));
    }

    /**
     * Show the form for creating a new resource.
     *  添加课程识图
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $tags = Tag::get();
        return view('admin.lesson.create',compact('tags'));
    }

    /**
     * Store a newly created resource in storage.
     *  保存新增课程的数据
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request,Lesson $lesson)
    {
        if ($request['videos']==''){
            flash('请添加视频')->overlay();
            return back();
        }
        if ($request['tags']==''){
            flash('请添加标签')->overlay();
            return back();
        }
        $lesson['title']=$request['title'];
        $lesson['name']=$request['name'];
        $lesson['introduce']=$request['introduce'];
        $lesson['preview']=$request['preview'];
        $lesson['iscommend']=$request['iscommend'];
        $lesson['ishot']=$request['ishot'];
        $lesson['click']=$request['click'];
        $lesson->save();

        $videos=json_decode($request['videos'], true);
        foreach ($videos as $video){
            $lesson->videos()->create([
                'title'=>$video['title'],
                'path'=>$video['path'],
            ]);
        }

        foreach ($request->input('tags') as $v) {
            $lesson->lessonTag()->create([
                'tag_id' => $v,
            ]);
        }
        flash('视频添加成功')->overlay();
        return redirect('/admin/lesson');

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Lesson $lesson)
    {
        //获取所有标签数据并分配
        $tags = Tag::get();
        //获取当前课程的标签数据
        $lessonTags = LessonTag::where('lesson_id',$lesson['id'])->pluck('tag_id')->toArray();
        //获取当前课程的所有视频数据
        //由于前台使用的视频数据是js的数组,我们应该将获取到的视频数据转成json数据,然后再分配
        $lessons = $lesson->videos->toArray();
        foreach ($lessons as $k => $v){
            $lessons[$k]['sid'] = $v['id'];
        }
        $videos = json_encode($lessons);
        //加载编辑模板
        return view('admin.lesson.edit',compact('tags','lesson','lessonTags','videos'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {

        if ($request['tags']==''){
            flash('请添加标签')->overlay();
            return back();
        }
        if ($request['videos']==''){
            flash('请添加视频')->overlay();
            return back();
        }
        $lesson=Lesson::find($id);
        $lesson['title']=$request['title'];
        $lesson['name']=$request['name'];
        $lesson['introduce']=$request['introduce'];
        $lesson['preview']=$request['preview'];
        $lesson['iscommend']=$request['iscommend'];
        $lesson['ishot']=$request['ishot'];
        $lesson['click']=$request['click'];
        $lesson->save();
        Video::where('lesson_id',$lesson['id'])->delete();

        $videos=json_decode($request['videos'], true);
        foreach ($videos as $video){
            $lesson->videos()->create([
                'title'=>$video['title'],
                'path'=>$video['path'],
            ]);
        }
        LessonTag::where('lesson_id',$lesson['id'])->delete();

        foreach ($request->input('tags') as $v) {
            $lesson->lessonTag()->create([
                'tag_id' => $v,
            ]);
        }
        flash('视频修改成功')->overlay();
        return redirect('/admin/lesson');

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Lesson::destroy($id);
        Video::where('lesson_id',$id)->delete();
        return $this->success('删除视频成功');
    }
}
